package com.avic.szzx.controller;


import com.avic.szzx.common.RequestObj;
import com.avic.szzx.controller.bean.quality_tracing.EquipmentInformationCorrespondence;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class AdapterController {
    @Autowired
    private ApplicationContext applicationcontext;

    public Object myTest1() {
        return "this is test request!";
    }
    public Object myTest2(String name) {
        String str = "name is:" + name;
        return str;
    }
    public Object myTest3(String name,String age) {
        String str = "name is:" + name + "age is:" + age;
        return str;
    }

    @GetMapping("/create1")
    @ResponseBody
    public String create() throws NoSuchMethodException {

        RequestMappingHandlerMapping bean = applicationcontext.getBean(RequestMappingHandlerMapping.class);
        AdapterController bean1 = applicationcontext.getBean(AdapterController.class);
        RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/test1")
                .methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo, bean1, AdapterController.class.getDeclaredMethod("myTest1"));
        return "成功创建api";
    }
    @GetMapping("/create2")
    @ResponseBody
    public String create2(@RequestParam String value) throws NoSuchMethodException {

        RequestMappingHandlerMapping bean = applicationcontext.getBean(RequestMappingHandlerMapping.class);
        AdapterController bean1 = applicationcontext.getBean(AdapterController.class);
        RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/test2")
                .params(new String[]{value})
                .methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo, bean1, AdapterController.class.getDeclaredMethod("myTest2", String.class));
        return "成功创建api";
    }
    @PostMapping("/create3")
    @ResponseBody
    public String create3(@RequestBody List<String> datas) throws NoSuchMethodException {

        RequestMappingHandlerMapping bean = applicationcontext.getBean(RequestMappingHandlerMapping.class);
        AdapterController bean1 = applicationcontext.getBean(AdapterController.class);
        String[] datas2 = datas.toArray(new String[datas.size()]);
        RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/test3")
                .params(datas2)
                .methods(RequestMethod.POST).build();
        bean.registerMapping(requestMappingInfo, bean1, AdapterController.class.getDeclaredMethod("myTest3",String.class,String.class));
        return "成功创建api";
    }

}
